Rename "runner" to "controller".

This is done in preparation for adding an ability for an agent to
enable or disable other agents other than running.

Akinori MUSHA vor 10 Jahren
Ursprung
Commit
de5619fb64

+ 2 - 2
app/assets/javascripts/application.js.coffee.erb

@@ -170,7 +170,7 @@ $(document).ready ->
170 170
         else
171 171
           hideLinks()
172 172
 
173
-        if json.can_run_other_agents
173
+        if json.can_control_other_agents
174 174
           showChains()
175 175
         else
176 176
           hideChains()
@@ -206,7 +206,7 @@ $(document).ready ->
206 206
       hideLinks()
207 207
 
208 208
   if $(".chain-region")
209
-    if $(".chain-region").data("can-run-other-agents") == true
209
+    if $(".chain-region").data("can-control-other-agents") == true
210 210
       showChains()
211 211
     else
212 212
       hideChains()

+ 27 - 0
app/concerns/agent_controller_concern.rb

@@ -0,0 +1,27 @@
1
+module AgentControllerConcern
2
+  extend ActiveSupport::Concern
3
+
4
+  def default_options
5
+    {
6
+      'action' => 'run',
7
+    }
8
+  end
9
+
10
+  def control_action
11
+    options['action'] || 'run'
12
+  end
13
+
14
+  def control_targets!
15
+    targets.active.each { |target|
16
+      begin
17
+        case control_action
18
+        when 'run'
19
+          log "Agent run queued for '#{target.name}'"
20
+          Agent.async_check(target.id)
21
+        end
22
+      rescue => e
23
+        log "Failed to #{control_action} '#{target.name}': #{e.message}"
24
+      end
25
+    }
26
+  end
27
+end

+ 3 - 3
app/helpers/agent_helper.rb

@@ -19,10 +19,10 @@ module AgentHelper
19 19
   def agent_schedule(agent, delimiter = ', ')
20 20
     return 'n/a' unless agent.can_be_scheduled?
21 21
 
22
-    runners = agent.runners
22
+    controllers = agent.controllers
23 23
     [
24
-      *(CGI.escape_html(agent.schedule.humanize.titleize) unless agent.schedule == 'never' && agent.runners.count > 0),
25
-      *runners.map { |agent| link_to(agent.name, agent_path(agent)) },
24
+      *(CGI.escape_html(agent.schedule.humanize.titleize) unless agent.schedule == 'never' && agent.controllers.count > 0),
25
+      *controllers.map { |agent| link_to(agent.name, agent_path(agent)) },
26 26
     ].join(delimiter).html_safe
27 27
   end
28 28
 end

+ 4 - 4
app/helpers/dot_helper.rb

@@ -138,9 +138,9 @@ module DotHelper
138 138
       def agent_edge(agent, receiver)
139 139
         edge(agent_id[agent],
140 140
              agent_id[receiver],
141
-             style: ('dashed' unless agent.can_run_other_agents? || !receiver.propagate_immediately?),
142
-             label: (' runs ' if agent.can_run_other_agents?),
143
-             arrowhead: ('empty' if agent.can_run_other_agents?),
141
+             style: ('dashed' unless agent.can_control_other_agents? || !receiver.propagate_immediately?),
142
+             label: (" #{agent.control_action}s " if agent.can_control_other_agents?),
143
+             arrowhead: ('empty' if agent.can_control_other_agents?),
144 144
              color: (@disabled if agent.disabled? || receiver.disabled?))
145 145
       end
146 146
 
@@ -162,7 +162,7 @@ module DotHelper
162 162
 
163 163
           [
164 164
             *agent.receivers,
165
-            *(agent.targets if agent.can_run_other_agents?)
165
+            *(agent.targets if agent.can_control_other_agents?)
166 166
           ].each { |receiver|
167 167
             agent_edge(agent, receiver) if agents.include?(receiver)
168 168
           }

+ 13 - 17
app/models/agent.rb

@@ -25,14 +25,14 @@ class Agent < ActiveRecord::Base
25 25
 
26 26
   EVENT_RETENTION_SCHEDULES = [["Forever", 0], ["1 day", 1], *([2, 3, 4, 5, 7, 14, 21, 30, 45, 90, 180, 365].map {|n| ["#{n} days", n] })]
27 27
 
28
-  attr_accessible :options, :memory, :name, :type, :schedule, :runner_ids, :target_ids, :disabled, :source_ids, :scenario_ids, :keep_events_for, :propagate_immediately
28
+  attr_accessible :options, :memory, :name, :type, :schedule, :controller_ids, :target_ids, :disabled, :source_ids, :scenario_ids, :keep_events_for, :propagate_immediately
29 29
 
30 30
   json_serialize :options, :memory
31 31
 
32 32
   validates_presence_of :name, :user
33 33
   validates_inclusion_of :keep_events_for, :in => EVENT_RETENTION_SCHEDULES.map(&:last)
34 34
   validate :sources_are_owned
35
-  validate :runners_are_owned
35
+  validate :controllers_are_owned
36 36
   validate :targets_are_owned
37 37
   validate :scenarios_are_owned
38 38
   validate :validate_schedule
@@ -54,10 +54,10 @@ class Agent < ActiveRecord::Base
54 54
   has_many :links_as_receiver, :dependent => :delete_all, :foreign_key => "receiver_id", :class_name => "Link", :inverse_of => :receiver
55 55
   has_many :sources, :through => :links_as_receiver, :class_name => "Agent", :inverse_of => :receivers
56 56
   has_many :receivers, :through => :links_as_source, :class_name => "Agent", :inverse_of => :sources
57
-  has_many :chains_as_runner, dependent: :delete_all, foreign_key: 'runner_id', class_name: 'Chain', inverse_of: :runner
57
+  has_many :chains_as_controller, dependent: :delete_all, foreign_key: 'controller_id', class_name: 'Chain', inverse_of: :controller
58 58
   has_many :chains_as_target, dependent: :delete_all, foreign_key: 'target_id', class_name: 'Chain', inverse_of: :target
59
-  has_many :runners, through: :chains_as_target, class_name: "Agent", inverse_of: :targets
60
-  has_many :targets, through: :chains_as_runner, class_name: "Agent", inverse_of: :runners
59
+  has_many :controllers, through: :chains_as_target, class_name: "Agent", inverse_of: :targets
60
+  has_many :targets, through: :chains_as_controller, class_name: "Agent", inverse_of: :controllers
61 61
   has_many :scenario_memberships, :dependent => :destroy, :inverse_of => :agent
62 62
   has_many :scenarios, :through => :scenario_memberships, :inverse_of => :agents
63 63
 
@@ -182,8 +182,8 @@ class Agent < ActiveRecord::Base
182 182
     !cannot_create_events?
183 183
   end
184 184
 
185
-  def can_run_other_agents?
186
-    self.class.can_run_other_agents?
185
+  def can_control_other_agents?
186
+    self.class.can_control_other_agents?
187 187
   end
188 188
 
189 189
   def log(message, options = {})
@@ -228,8 +228,8 @@ class Agent < ActiveRecord::Base
228 228
     errors.add(:sources, "must be owned by you") unless sources.all? {|s| s.user == user }
229 229
   end
230 230
   
231
-  def runners_are_owned
232
-    errors.add(:runners, "must be owned by you") unless runners.all? {|s| s.user == user }
231
+  def controllers_are_owned
232
+    errors.add(:controllers, "must be owned by you") unless controllers.all? {|s| s.user == user }
233 233
   end
234 234
 
235 235
   def targets_are_owned
@@ -267,7 +267,7 @@ class Agent < ActiveRecord::Base
267 267
 
268 268
   class << self
269 269
     def build_clone(original)
270
-      new(original.slice(:type, :options, :schedule, :runner_ids, :source_ids, :keep_events_for, :propagate_immediately)) { |clone|
270
+      new(original.slice(:type, :options, :schedule, :controller_ids, :source_ids, :keep_events_for, :propagate_immediately)) { |clone|
271 271
         # Give it a unique name
272 272
         2.upto(count) do |i|
273 273
           name = '%s (%d)' % [original.name, i]
@@ -308,12 +308,8 @@ class Agent < ActiveRecord::Base
308 308
       !!@cannot_receive_events
309 309
     end
310 310
 
311
-    def can_run_other_agents!
312
-      @can_run_other_agents = true
313
-    end
314
-
315
-    def can_run_other_agents?
316
-      @can_run_other_agents
311
+    def can_control_other_agents?
312
+      include? AgentControllerConcern
317 313
     end
318 314
 
319 315
     # Find all Agents that have received Events since the last execution of this method.  Update those Agents with
@@ -425,7 +421,7 @@ class AgentDrop
425 421
     :sources,
426 422
     :receivers,
427 423
     :schedule,
428
-    :runners,
424
+    :controllers,
429 425
     :targets,
430 426
     :disabled,
431 427
     :keep_events_for,

+ 6 - 6
app/models/agents/scheduler_agent.rb

@@ -2,10 +2,11 @@ require 'rufus-scheduler'
2 2
 
3 3
 module Agents
4 4
   class SchedulerAgent < Agent
5
+    include AgentControllerConcern
6
+
5 7
     cannot_be_scheduled!
6 8
     cannot_receive_events!
7 9
     cannot_create_events!
8
-    can_run_other_agents!
9 10
 
10 11
     description <<-MD
11 12
       This agent periodically triggers a run of each target Agent according to a user-defined schedule.
@@ -37,7 +38,9 @@ module Agents
37 38
     MD
38 39
 
39 40
     def default_options
40
-      { 'schedule' => '0 * * * *' }
41
+      super.update({
42
+        'schedule' => '0 * * * *',
43
+      })
41 44
     end
42 45
 
43 46
     def working?
@@ -45,10 +48,7 @@ module Agents
45 48
     end
46 49
 
47 50
     def check!
48
-      targets.active.each { |target|
49
-        log "Agent run queued for '#{target.name}'"
50
-        Agent.async_check(target.id)
51
-      }
51
+      control_targets!
52 52
     end
53 53
 
54 54
     def validate_options

+ 3 - 3
app/models/chain.rb

@@ -1,7 +1,7 @@
1
-# A Chain connects Agents in a run chain from the `runner` to the `target`.
1
+# A Chain connects Agents in a run chain from the `controller` to the `target`.
2 2
 class Chain < ActiveRecord::Base
3
-  attr_accessible :runner_id, :target_id
3
+  attr_accessible :controller_id, :target_id
4 4
 
5
-  belongs_to :runner, class_name: 'Agent', inverse_of: :chains_as_runner
5
+  belongs_to :controller, class_name: 'Agent', inverse_of: :chains_as_controller
6 6
   belongs_to :target, class_name: 'Agent', inverse_of: :chains_as_target
7 7
 end

+ 12 - 12
app/views/agents/_form.html.erb

@@ -43,26 +43,26 @@
43 43
           <div class="schedule-region" data-can-be-scheduled="<%= @agent.can_be_scheduled? %>">
44 44
             <div class="can-be-scheduled">
45 45
               <div class="form-group">
46
-                <%= f.label :runners %>
47
-                <span class="glyphicon glyphicon-question-sign hover-help" data-content="Other than the system-defined schedule above, this agent may be run by user-defined ScheduleAgents."></span>
48
-                <% eventRunners = current_user.agents.select(&:can_run_other_agents?) %>
49
-                <%= f.select(:runner_ids,
50
-                               options_for_select(eventRunners.map {|s| [s.name, s.id] },
51
-                                                  @agent.runner_ids),
52
-                               {}, { multiple: true, size: 5, class: 'select2 form-control' }) %>
46
+                <%= f.label :controllers %>
47
+                <span class="glyphicon glyphicon-question-sign hover-help" data-content="Other than the system-defined schedule above, this agent may be run by user-defined Agents."></span>
48
+                <% eventControllers = current_user.agents.select(&:can_control_other_agents?) %>
49
+                <%= f.select(:controller_ids,
50
+                             options_for_select(eventControllers.map {|s| [s.name, s.id] },
51
+                                                @agent.controller_ids),
52
+                             {}, { multiple: true, size: 5, class: 'select2 form-control' }) %>
53 53
               </div>
54 54
             </div>
55 55
           </div>
56 56
 
57
-          <div class="chain-region" data-can-run-other-agents="<%= @agent.can_run_other_agents? %>">
58
-            <div class="can-run-other-agents">
57
+          <div class="chain-region" data-can-control-other-agents="<%= @agent.can_control_other_agents? %>">
58
+            <div class="can-control-other-agents">
59 59
               <div class="form-group">
60 60
                 <%= f.label :targets %>
61 61
                 <% eventTargets = current_user.agents.select(&:can_be_scheduled?) %>
62 62
                 <%= f.select(:target_ids,
63
-                               options_for_select(eventTargets.map {|s| [s.name, s.id] },
64
-                                                  @agent.target_ids),
65
-                               {}, { multiple: true, size: 5, class: 'select2 form-control' }) %>
63
+                             options_for_select(eventTargets.map {|s| [s.name, s.id] },
64
+                                                @agent.target_ids),
65
+                             {}, { multiple: true, size: 5, class: 'select2 form-control' }) %>
66 66
               </div>
67 67
             </div>
68 68
           </div>

+ 1 - 1
app/views/agents/show.html.erb

@@ -134,7 +134,7 @@
134 134
               </p>
135 135
             <% end %>
136 136
 
137
-            <% if @agent.can_run_other_agents? %>
137
+            <% if @agent.can_control_other_agents? %>
138 138
               <p>
139 139
                 <b>Targets:</b>
140 140
                 <% if (agents = @agent.targets).length > 0 %>

+ 5 - 0
db/migrate/20140822085519_rename_runner_to_controller.rb

@@ -0,0 +1,5 @@
1
+class RenameRunnerToController < ActiveRecord::Migration
2
+  def change
3
+    rename_column :chains, :runner_id, :controller_id
4
+  end
5
+end

+ 3 - 3
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20140821115428) do
14
+ActiveRecord::Schema.define(version: 20140822085519) do
15 15
 
16 16
   create_table "agent_logs", force: true do |t|
17 17
     t.integer  "agent_id",                                       null: false
@@ -51,13 +51,13 @@ ActiveRecord::Schema.define(version: 20140821115428) do
51 51
   add_index "agents", ["user_id", "created_at"], name: "index_agents_on_user_id_and_created_at", using: :btree
52 52
 
53 53
   create_table "chains", force: true do |t|
54
-    t.integer  "runner_id"
54
+    t.integer  "controller_id"
55 55
     t.integer  "target_id"
56 56
     t.datetime "created_at"
57 57
     t.datetime "updated_at"
58 58
   end
59 59
 
60
-  add_index "chains", ["runner_id", "target_id"], name: "index_chains_on_runner_id_and_target_id", unique: true, using: :btree
60
+  add_index "chains", ["controller_id", "target_id"], name: "index_chains_on_controller_id_and_target_id", unique: true, using: :btree
61 61
   add_index "chains", ["target_id"], name: "index_chains_on_target_id", using: :btree
62 62
 
63 63
   create_table "delayed_jobs", force: true do |t|

+ 6 - 6
spec/models/agent_spec.rb

@@ -497,15 +497,15 @@ describe Agent do
497 497
         agent.should have(0).errors_on(:sources)
498 498
       end
499 499
 
500
-      it "should not allow runner agents owned by other people" do
500
+      it "should not allow controller agents owned by other people" do
501 501
         agent = Agents::SomethingSource.new(:name => "something")
502 502
         agent.user = users(:bob)
503
-        agent.runner_ids = [agents(:bob_weather_agent).id]
504
-        agent.should have(0).errors_on(:runners)
505
-        agent.runner_ids = [agents(:jane_weather_agent).id]
506
-        agent.should have(1).errors_on(:runners)
503
+        agent.controller_ids = [agents(:bob_weather_agent).id]
504
+        agent.should have(0).errors_on(:controllers)
505
+        agent.controller_ids = [agents(:jane_weather_agent).id]
506
+        agent.should have(1).errors_on(:controllers)
507 507
         agent.user = users(:jane)
508
-        agent.should have(0).errors_on(:runners)
508
+        agent.should have(0).errors_on(:controllers)
509 509
       end
510 510
 
511 511
       it "should not allow target agents owned by other people" do